|
1
|
|
|
import { Injectable } from "@angular/core"; |
|
2
|
|
|
import { |
|
3
|
|
|
ActivatedRouteSnapshot, |
|
4
|
|
|
CanActivate, |
|
5
|
|
|
CanActivateChild, |
|
6
|
|
|
Router, |
|
7
|
|
|
RouterStateSnapshot, |
|
8
|
|
|
} from "@angular/router"; |
|
9
|
|
|
import { Observable } from "rxjs"; |
|
10
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
|
11
|
|
|
import { UniFirebaseLoginConfigProvider } from "../config/uni-firebase-login-config-provider"; |
|
12
|
|
|
import { UserModel } from "../model/user-model"; |
|
13
|
|
|
import { BaseAuthService } from "../services/base-auth-service"; |
|
14
|
|
|
|
|
15
|
|
|
@Injectable({ |
|
16
|
|
|
providedIn: "root", |
|
17
|
|
|
}) |
|
18
|
|
|
export class NonAuthGuard<User extends UserModel = UserModel> |
|
19
|
|
|
implements CanActivate, CanActivateChild { |
|
20
|
|
|
protected config: UniFirebaseLoginConfig; |
|
21
|
|
|
|
|
22
|
|
|
public constructor( |
|
23
|
|
|
protected auth: BaseAuthService<User>, |
|
24
|
|
|
protected router: Router, |
|
25
|
|
|
configProvider: UniFirebaseLoginConfigProvider, |
|
26
|
|
|
) { |
|
27
|
|
|
this.config = configProvider.config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public canActivate( |
|
31
|
|
|
next: ActivatedRouteSnapshot, |
|
32
|
|
|
state: RouterStateSnapshot, |
|
33
|
|
|
): Observable<boolean> { |
|
34
|
|
|
return new Observable(subscriber => { |
|
35
|
|
|
this.auth.userInitialized$.subscribe(initialized => { |
|
36
|
|
|
if (initialized) { |
|
37
|
|
|
this.auth.user$.subscribe(user => { |
|
38
|
|
|
const loggedIn = !!user; |
|
39
|
|
|
if (loggedIn) { |
|
40
|
|
|
// Access denied |
|
41
|
|
|
const redirectTo = this.config.afterSignInPage; |
|
42
|
|
|
console.log( |
|
43
|
|
|
`Cannot be logged in on this page, redirecting to: ${redirectTo}`, |
|
44
|
|
|
); |
|
45
|
|
|
this.router.navigate([redirectTo]); |
|
46
|
|
|
subscriber.next(false); |
|
47
|
|
|
} else { |
|
48
|
|
|
subscriber.next(true); |
|
49
|
|
|
} |
|
50
|
|
|
subscriber.complete(); |
|
51
|
|
|
}); |
|
52
|
|
|
} |
|
53
|
|
|
}); |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public canActivateChild( |
|
58
|
|
|
next: ActivatedRouteSnapshot, |
|
59
|
|
|
state: RouterStateSnapshot, |
|
60
|
|
|
): Observable<boolean> { |
|
61
|
|
|
return this.canActivate(next, state); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|